import React, { FC, useEffect, useState, useRef } from 'react';
import { Form, Input, Button, Radio, RadioChangeEvent, Switch, message, List } from 'antd';
import '../../static/css/administrator/admAdd.css'
import { useParams } from 'react-router';
import { getDetail,removeAdm } from '../../apis/administrator'
import {getAdds} from '../../apis/administrator'
import { useNavigate } from 'react-router';


const AdmAdd: FC<any> = (props: any): JSX.Element => {
  const navigate = useNavigate();
  const { id, title } = useParams();
  const [titles, setTitles] = useState(title)
  const [xiugai,getxiugai]=useState({})
  // 修改列表回显
  const formRef: any = useRef()
  const getData = async () => {
    const res: any = await getDetail(Number(id))
    console.log(res);
    formRef.current.setFieldsValue(res.data)
  }
  const getAdd=()=>{
    console.log('123');
  }
  // 添加
  const onFinish =  (props:any) =>{
    console.log(props);
    getxiugai(props)
    if(title === '3'){
      getAdds(props).then((res:any)=>{
        console.log(res);
        if(res.code=='00000'){
          message.success('添加成功')
          navigate('/index/administrator')
        }
       })    
    }
  }

  // 提交 修改
  const Addlist = async ()=>{
    // alert(123)
    console.log(id,xiugai);
    
    const res: any = await removeAdm({admin_id:id,formdata:xiugai})
    console.log(123456,res);
    if(res.code=='00000'){
      navigate('/index/administrator')
    }
  }
  
  useEffect(() => {
    if(title==='3'){
      getAdd()
    }else{
      getData()
   
    }
  }, [id])



  return (
    <div>
      <Form
        ref={formRef}
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 14 }}
        onFinish={onFinish}
        layout="horizontal">
        <Form.Item name="admin_name" label="管理员名称" rules={[{ required: true }]}>
          <Input placeholder='请输入管理员名称' />
        </Form.Item>
        <Form.Item name="real_name" label="真实姓名" rules={[{ required: true }]}>
          <Input placeholder='请输入新的真实姓名' />
        </Form.Item>
        <Form.Item name="email" label="邮箱" rules={[{ required: true }]}>
          <Input placeholder='请输入邮箱' />
        </Form.Item>
        <Form.Item name="admin_name" label="部门" rules={[{ required: true }]}>
          <Input placeholder='请输入管理员名称' />
        </Form.Item>
        <Form.Item name="mobile" label="手机号" rules={[{ required: true }]}>
          <Input placeholder='请输入手机号' />
        </Form.Item>
        <Form.Item name="role" label="角色" rules={[{ required: true }]}>
          <Radio.Group>
            <Radio value={2}>经管负责人</Radio>
            <Radio value={1}>部门负责人</Radio>
          </Radio.Group>
          {/* <Radio.Group options={plainOptions} onChange={onChange1} value={value1} /> */}
          {/* <Radio value={1} >经管负责人</Radio> */}
          {/* <Radio value={2}>经管负责人</Radio> */}
        </Form.Item>
        <Form.Item name="pwd" label="管理员密码" rules={[{ required: true }]}>
          <Input placeholder='请输入管理员密码' />
        </Form.Item>
        <Form.Item name="is_use" valuePropName="checked" label="是否启用" rules={[{ required: true }]}>
          <Switch />
        </Form.Item>
        <Form.Item name="repwd" label="确认密码">
          <Input placeholder='请再次输入密码' />
        </Form.Item>
        <Form.Item name="remarks" label="备注" rules={[{ required: true }]}>
          <Input placeholder='请输入管理员名称' />
        </Form.Item>
        <Form.Item>
        </Form.Item>
        <Form.Item>
          {
            // 添加
            titles === '3' && <div className='but'>
            <Button type="primary" htmlType="submit" size='middle'>
           重置
         </Button>
          <Button type="primary" htmlType="submit" size='middle'>
           添加
          </Button>
            </div>
          }

          {
            // 修改
             titles === '2' && <div className='but'>
               <Button type="primary" htmlType="submit" size='middle'>
              重置
            </Button>
             <Button onClick={Addlist} type="primary" htmlType="submit" size='middle'>
              提交
             </Button>
           </div>
          }
        </Form.Item>
      </Form>
    </div>
  )
}
export default AdmAdd;